package com.letoo.dragon.core.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.letoo.dragon.common.exception.BusinessException;
import com.letoo.dragon.core.service.SystemCodeService;
import com.letoo.dragon.dataaccess.dao.cahce.SystemCodeCacheDAO;
import com.letoo.dragon.dataaccess.dao.db.BaseDBDAO;
import com.letoo.dragon.dataaccess.dao.db.SystemCodeDBDAO;
import com.letoo.dragon.dataaccess.domain.dbo.SystemCodeDO;

@Service("systemCodeService")
public class SystemCodeServiceImpl extends BaseServiceImpl<SystemCodeDO> implements SystemCodeService {

  /**
   * 日志
   */
  private final Logger log = LoggerFactory.getLogger(getClass());

  @Autowired
  private SystemCodeDBDAO systemCodeDBDAO;

  @Autowired
  private SystemCodeCacheDAO systemCodeCacheDAO;

  @Override
  public boolean initSystemCode() {
    boolean flag = true;
    try {
      // 从数据库中读取所有的系统代码
      List<SystemCodeDO> systemCodeList = systemCodeDBDAO.findAll();

      if (systemCodeList != null && !systemCodeList.isEmpty()) {
        flag = systemCodeCacheDAO.batchAddSystemCode(systemCodeList, true);
      }
    } catch (Exception e) {
      log.error("InitSystemCode error", e);
      flag = false;
    }

    return flag;
  }

  @Override
  public Long addSystemCode(String username, String codeType, String codeTypeName, String codeValue,
      String codeValueName, String remark, Integer orderNo) {
    // 参数校验
    if (StringUtils.isEmpty(codeType) || StringUtils.isEmpty(codeTypeName) || StringUtils.isEmpty(codeValue)
        || StringUtils.isEmpty(codeValueName)) {
      throw new BusinessException("参数错误");
    }

    // 数据库插入一条新的数据
    SystemCodeDO systemCode = new SystemCodeDO();
    systemCode.setCodeType(codeType);
    systemCode.setCodeTypeName(codeTypeName);
    systemCode.setCodeValue(codeValue);
    systemCode.setCodeValueName(codeValueName);
    systemCode.setRemark(remark);
    systemCode.setIsDefault(false);
    systemCode.setIsActive(true);
    systemCode.setCreateUser(username);
    systemCode.setUpdateUser(username);
    systemCode.setOrderNo(orderNo);
    Integer num = systemCodeDBDAO.save(systemCode);

    // 更新缓存
    if (num.longValue() > 0) {
      systemCodeCacheDAO.addSystemCode(systemCode);
    }

    return systemCode.getId();
  }

  @Override
  public boolean modifySystemCode(String username, Integer id, String codeTypeName, String codeValueName, String remark,
      Integer orderNo) {
    // 参数校验
    if (StringUtils.isEmpty(codeTypeName) || StringUtils.isEmpty(codeValueName)) {
      throw new BusinessException("参数错误");
    }

    SystemCodeDO oldSystemCode = systemCodeDBDAO.findById(id);

    if (oldSystemCode == null) {
      throw new BusinessException("系统代码不存在");
    }

    SystemCodeDO newSystemCode = new SystemCodeDO();
    newSystemCode.setId(id);
    newSystemCode.setCodeType(oldSystemCode.getCodeType());
    newSystemCode.setCodeValue(oldSystemCode.getCodeValue());
    newSystemCode.setCodeTypeName(codeTypeName);
    newSystemCode.setCodeValueName(codeValueName);
    newSystemCode.setIsDefault(oldSystemCode.getIsDefault());
    newSystemCode.setRemark(remark);
    newSystemCode.setUpdateUser(username);
    newSystemCode.setOrderNo(orderNo);
    systemCodeDBDAO.update(newSystemCode);
    systemCodeCacheDAO.updateSystemCode(oldSystemCode, newSystemCode);

    return true;
  }

  @Override
  public List<SystemCodeDO> getSystemCodeList(String codeType) {
    // 参数校验
    if (StringUtils.isEmpty(codeType)) {
      throw new BusinessException("参数错误");
    }
    // redis查询
    List<SystemCodeDO> systemCodeList = systemCodeCacheDAO.getSystemCodes(codeType);

    if (systemCodeList == null || systemCodeList.isEmpty()) {
      systemCodeList = systemCodeDBDAO.findByCodeType(codeType);

      if (systemCodeList != null && !systemCodeList.isEmpty()) {
        systemCodeCacheDAO.batchAddSystemCode(systemCodeList, false);
      }
    }

    return systemCodeList;
  }

  @Override
  public SystemCodeDO getSystemCode(String codeType, String codeValue) {
    if (StringUtils.isEmpty(codeType) || StringUtils.isEmpty(codeValue)) {
      return null;
    }

    SystemCodeDO systemCode = systemCodeDBDAO.findByCodeTypeAndCodeValue(codeType, codeValue);
    return systemCode;
  }

  @Override
  protected BaseDBDAO<SystemCodeDO> getDAO() {
    return systemCodeDBDAO;
  }

  @Override
  public SystemCodeDO getDefaultSystemCode(String codeType) {
    return systemCodeDBDAO.findDefaultCodeValue(codeType);
  }

  @Override
  public Map<String, List<SystemCodeDO>> getSystemCodeMapWithList(String... codeTypes) {
    // 参数校验
    if (codeTypes == null) {
      throw new BusinessException("代码类型不能为空");
    }

    Map<String, List<SystemCodeDO>> systemCodeMap = new HashMap<String, List<SystemCodeDO>>();

    for (String codeType : codeTypes) {
      if (StringUtils.isBlank(codeType)) {
        throw new BusinessException("代码类型不能为空");
      }

      systemCodeMap.put(codeType, getSystemCodeList(codeType));
    }

    return systemCodeMap;
  }

  @Override
  public Map<String, SystemCodeDO> getSystemCodeMap(String... codeTypes) {
    // 参数校验
    if (codeTypes == null) {
      throw new BusinessException("代码类型不能为空");
    }

    Map<String, SystemCodeDO> systemCodeMap = new HashMap<String, SystemCodeDO>();

    for (String codeType : codeTypes) {
      if (StringUtils.isBlank(codeType)) {
        throw new BusinessException("代码类型不能为空");
      }

      List<SystemCodeDO> systemCodeList = getSystemCodeList(codeType);

      for (SystemCodeDO systemCode : systemCodeList) {
        systemCodeMap.put(systemCode.getCodeValue(), systemCode);
      }
    }

    return systemCodeMap;
  }
}
